package com.feib.soeasy.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.feib.soeasy.model.CodeTable;
import com.feib.soeasy.service.CodeTableService;


/**
 * @title (#)CodeTableUtil.java<br>
 * @description 從SOEZ_CODE_TABLE中獲得數據:<BR>
 * <ul> 
 * <li>getList:根據CODE_TYPE從SOEZ_CODE_TABLE中獲得List&lt;LabelValue&gt
 * <li>getMap:根據CODE_VALUE從SOEZ_CODE_TABLE中獲得CODE_DESC
 * </ul><br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/15 create by Jimmy Liu<br>
 */
public class CodeTableUtil {

    public static final String CODE_KIND_CHANNEL = "CHANEL";
    
    public static final String CODE_KIND_UPLOAD_FILE_STATUS = "UPLOAD_FILE_STATUS";
    
    public static final String CODE_KIND_PAYMENT_STATUS = "PAYMENT_STATUS";
    
    public static final String CODE_KIND_PAGE_RECORD_SIZE = "PAGE_RECORD_SIZE";
    
    private static CodeTableUtil instance = null;

    private CodeTableService codeTableService;

    private Map<String, Map<String, String>> codesMap = null;
    
    private Map<String, Date> codeLoadTimeMap = null;

    private CodeTableUtil()
    {
        codesMap = new HashMap<String, Map<String, String>>();
        codeLoadTimeMap = new HashMap<String, Date>();
        codeTableService = (CodeTableService)SpringContextUtil.getBean("codeTableService");
    }

    public static CodeTableUtil getInstance()
    {
        if (null == instance )
        {
            synchronized(CodeTableUtil.class) {
                if (instance == null)
                    instance = new CodeTableUtil();
            }
        }
        return instance;
    }

    /**
     * 用代碼類別重新載入代碼
     * @param codeKind 代碼類別，不可為空
     */
    public void reload(String codeKind)
    {
        if (StringUtils.isEmpty(codeKind))
        {
            throw new RuntimeException("重新載入代碼時，代碼類別(codeKind)不可為空");
        }

        // 取得代碼資料
        Map<String, String> codes = codesMap.get(codeKind);
        if (null == codes)
        {
            codes = new HashMap<String, String>();
            codesMap.put(codeKind, codes);
        }

        // 清空
        codes.clear();

        //load by code kind
        List<CodeTable> codeTables = codeTableService.queryByCodeKind(codeKind);
        for(CodeTable codeTable : codeTables)
        {
            codes.put(codeTable.getCodeValue(), codeTable.getCodeName());
        }
        codeTables.clear();
        codeTables = null;
        
        // 更新時間
        codeLoadTimeMap.put(codeKind, new Date());
    }

    /**
     * 用代碼類別取出代碼
     * @param codeKind 代碼類別，不可為空
     * @return 代碼
     */
    public Map<String, String> populateCodes(String codeKind)
    {
        Date reloadTime = codeLoadTimeMap.get(codeKind);
        if (null == reloadTime)
        {
            reload(codeKind);
            
            return codesMap.get(codeKind);
        }
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(reloadTime);
        cal.add(Calendar.HOUR, 2);
        reloadTime = cal.getTime();
        
        if (new Date().after(reloadTime))
        {
            reload(codeKind);
        }
        
        return codesMap.get(codeKind);
    }

    public String getCodeName(String codeKind, String codeValue)
    {        
        // 取得代碼資料
        Map<String, String> codes = populateCodes(codeKind);
        
        String codeName = codes.get(codeValue);
        if (null == codeName)
        {
            codeName = "";
        }
        return codeName;
    }

}
